from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from db.base import init_db
from db.database import SessionLocal
from services import formula_service
import threading
import time
import os

app = FastAPI()

# 创建uploads目录（如果不存在）
uploads_dir = "uploads"
os.makedirs(uploads_dir, exist_ok=True)

# 挂载静态文件服务，用于访问上传的图片
app.mount("/uploads", StaticFiles(directory=uploads_dir), name="uploads")

@app.on_event("startup")
def on_startup():
    init_db()
    # Create a new DB session just for startup
    db = SessionLocal()
    formula_service.create_initial_formulas(db)
    db.close()

@app.get("/")
def read_root():
    return {"message": "Welcome to the Formula Copier API!"}

# Mount the API routers
from api.v1 import formula

app.include_router(formula.router, prefix="/api/v1/formulas", tags=["formulas"])

# Mount the submission router
from api.v1 import submission
app.include_router(submission.router, prefix="/api/v1/submissions", tags=["submissions"])

# Mount the admin router
from api.v1 import admin
app.include_router(admin.router, prefix="/api/v1/admin", tags=["admin"])


# Note: Admin UI can be started separately by running:
# python -c "from ui.admin_ui import launch_admin_ui; launch_admin_ui()"
